package com.geekjam.dingdongandroid.api;

import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.AsyncHttpResponseHandler;
import com.loopj.android.http.RequestParams;

/**
 * by SEGFault Xiawei
 * Date: 3/9/13 Time: 2:19 PM
 */
public abstract class Proxy {

    public static final String BASE_URL = "http://dingdongeveryday.appspot.com";

    protected static AsyncHttpClient _client = new AsyncHttpClient();
    protected static int _userId;
    protected static int _venueId;

    public static void init(Context applicationContext, int user_id, int venue_id) {
        _userId = user_id;
        _venueId = venue_id;
        _client.addHeader("Cookie", String.format("userid=%d;vid=%d", user_id, venue_id));

    }

    public static boolean isNetworkAvailable(Context context) {
        ConnectivityManager connectivityManager = (ConnectivityManager)
                context.getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
        return activeNetworkInfo != null;
    }

    protected static void get(String url, RequestParams params, AsyncHttpResponseHandler responseHandler) {
        _client.get(getAbsoluteUrl(url), params, responseHandler);
    }

    protected static void post(String url, RequestParams params, AsyncHttpResponseHandler responseHandler) {
        _client.post(getAbsoluteUrl(url), params, responseHandler);
    }

    protected static void put(String url, RequestParams params, AsyncHttpResponseHandler responseHandler) {
        _client.put(getAbsoluteUrl(url), params, responseHandler);
    }

    protected static void syncGet(String url, RequestParams params, AsyncHttpResponseHandler responseHandler) {
        _client.syncGet(getAbsoluteUrl(url), params, responseHandler);
    }

    protected static String getAbsoluteUrl(String relativeUrl) {
        return BASE_URL + relativeUrl;
    }
}

